package financier.jfz.com.babycare;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jfz.quick.core.db.LightOrmHelper;
import com.jfz.quick.core.db.WhereBulider;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import financier.jfz.com.babycare.base.BaseActivity;
import financier.jfz.com.babycare.bean.BaseModle;
import financier.jfz.com.babycare.bean.TableMsgDataBean;

public class TableListActivity extends BaseActivity {
    RecyclerView recyclerView;
    List<TableMsgDataBean> data = new ArrayList<>();
    Adater adater;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modellist_layout);
        initUI();
    }

    private void initUI() {
        getToolbar().setMainTitle("数据统计列表");
        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adater = new Adater();
        recyclerView.setAdapter(adater);
        findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.title_dialog, null);
                final TextView textView = dialogView.findViewById(R.id.tv_name);
                new AlertDialog.Builder(view.getContext())
                        .setTitle("創建數據對象")
                        .setView(dialogView).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (TextUtils.isEmpty(textView.getText())) {
                            Toast.makeText(TableListActivity.this, "名称不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        selectModleDataPicker(textView.getText().toString());
                    }
                }).create().show();
            }
        });
        getData();
    }

   private void selectModleDataPicker(final String name){
     final   List<BaseModle> baseModleList=LightOrmHelper.getInstent().query(BaseModle.class);
       List<String> dataList=new ArrayList<>();
       for(BaseModle mBaseModle:baseModleList){
           dataList.add(mBaseModle.getName());
       }
       new AlertDialog.Builder(this).setTitle("选择数据模型")
               .setItems(dataList.toArray(new String[]{}), new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialogInterface, int i) {
                       BaseModle mBaseModle= baseModleList.get(i);
                       TableMsgDataBean arg=new TableMsgDataBean();
                       arg.id=UUID.randomUUID().toString();
                       arg.modleId=mBaseModle.getId();
                       arg.moldeName=mBaseModle.getName();
                       arg.MsgName=name;
                       LightOrmHelper.getInstent().save(arg);
                       startActivity(new Intent(TableListActivity.this, TableActivity.class).putExtra(TableActivity.MODLE, mBaseModle).putExtra(TableActivity.MSG,arg));
                   }
               }).create().show();
   }

    @Override
    public void startActivity(Intent intent) {
        super.startActivityForResult(intent,10000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        this.data.clear();
        getData();
    }

    private void getData() {
        List<TableMsgDataBean> baseModleList = LightOrmHelper.getInstent().query(TableMsgDataBean.class);
        data.addAll(baseModleList);
        adater.notifyDataSetChanged();
//        Toast.makeText(this, "size=" + baseModleList.size(), Toast.LENGTH_SHORT).show();
    }

    public class Adater extends RecyclerView.Adapter<TableListActivity.Holder> {

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.model_item_layout, viewGroup, false);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int i) {
           StringBuilder stringBuilder= new StringBuilder().append(data.get(i).MsgName).append("[").append(data.get(i).moldeName).append("]");
            holder.textView.setText(stringBuilder.toString());
        }

        @Override
        public int getItemCount() {
            return data.size();
        }
    }

    public class Holder extends RecyclerView.ViewHolder {
        TextView textView;

        public Holder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_name);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  List<BaseModle> baseModleList=  LightOrmHelper.getInstent().query(BaseModle.class,WhereBulider.creat().where("id=?",data.get(getAdapterPosition()).modleId));
                    startActivity(new Intent(TableListActivity.this, TableActivity.class).putExtra(TableActivity.MODLE, baseModleList.get(0)).putExtra(TableActivity.MSG, data.get(getAdapterPosition())));
                }
            });
        }
    }

}
